<template>
  <div class="map-view">
    <div class="map-header">
      <h1 class="map-title">🏝️ 数字探险岛</h1>
      <div class="player-info">
        <div class="stars-count">⭐ {{ gameStore.playerProgress.totalStars }}</div>
        <button class="backpack-btn" @click="showBackpack = true">
          🎒 我的背包
        </button>
      </div>
    </div>

    <div class="map-container" ref="mapContainer">
      <div class="map-content">
        <svg class="map-path" :viewBox="`0 0 ${mapWidth} ${mapHeight}`">
          <!-- 绘制关卡之间的路径 -->
          <path
            v-for="(level, index) in gameStore.levels.slice(0, -1)"
            :key="`path-${level.id}`"
            :d="getPathBetweenLevels(level, gameStore.levels[index + 1])"
            class="level-path"
            :class="{
              'level-path--unlocked': gameStore.levels[index + 1].status !== 'locked'
            }"
          />
        </svg>

        <!-- 关卡节点 -->
        <div
          v-for="level in gameStore.levels"
          :key="level.id"
          class="level-node"
          :class="{
            'level-node--locked': level.status === 'locked',
            'level-node--unlocked': level.status === 'unlocked',
            'level-node--completed': level.status === 'completed'
          }"
          :style="{
            left: `${level.position.x}px`,
            top: `${level.position.y}px`
          }"
          @click="handleLevelClick(level)"
        >
          <div class="level-icon">
            {{ getLevelIcon(level.type) }}
          </div>
          <div class="level-name">{{ level.name }}</div>
          <div v-if="level.status === 'completed'" class="level-stars">
            <span v-for="i in 3" :key="i" class="star" :class="{ 'star--filled': i <= level.stars }">
              ⭐
            </span>
          </div>
          <div v-if="level.status === 'locked'" class="level-lock">🔒</div>
        </div>
      </div>
    </div>

    <!-- 背包弹窗 -->
    <div v-if="showBackpack" class="modal-overlay" @click="showBackpack = false">
      <div class="modal-content backpack-modal" @click.stop>
        <h2>🎒 我的背包</h2>
        <div class="backpack-stats">
          <div class="stat-item">
            <span class="stat-label">总星星数：</span>
            <span class="stat-value">⭐ {{ gameStore.playerProgress.totalStars }}</span>
          </div>
          <div class="stat-item">
            <span class="stat-label">完成关卡：</span>
            <span class="stat-value">{{ gameStore.completedLevelsCount }} / {{ gameStore.levels.length }}</span>
          </div>
        </div>

        <h3>🏆 我的徽章</h3>
        <div class="badges-grid">
          <div
            v-for="badge in gameStore.availableBadges"
            :key="badge.id"
            class="badge-item"
            :class="{ 'badge-item--locked': !badge.unlocked }"
          >
            <div class="badge-icon">{{ badge.icon }}</div>
            <div class="badge-name">{{ badge.name }}</div>
            <div class="badge-desc">{{ badge.description }}</div>
          </div>
        </div>

        <button class="close-btn" @click="showBackpack = false">关闭</button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useGameStore } from '@/stores/game'
import type { Level } from '@/types/game'
import { LevelType } from '@/types/game'
import { soundManager } from '@/utils/sound'
import { createPulseEffect } from '@/utils/animation'

const router = useRouter()
const gameStore = useGameStore()

const showBackpack = ref(false)
const mapContainer = ref<HTMLElement>()
const mapWidth = 1200
const mapHeight = 500

onMounted(() => {
  // 加载游戏进度
  gameStore.loadProgress()
})

function getLevelIcon(type: LevelType): string {
  const icons: Record<LevelType, string> = {
    [LevelType.NUMBER_RECOGNITION]: '🔢',
    [LevelType.SHAPE_MATCHING]: '🔷',
    [LevelType.COMPARE_SIZE]: '⚖️',
    [LevelType.ADDITION]: '➕',
    [LevelType.SUBTRACTION]: '➖',
    [LevelType.PATTERN]: '🧩',
    [LevelType.LOGIC]: '🧠',
    [LevelType.TIME]: '🕐',
    [LevelType.MONEY]: '💰'
  }
  return icons[type] || '❓'
}

function getPathBetweenLevels(level1: Level, level2: Level): string {
  const x1 = level1.position.x + 50 // 节点中心
  const y1 = level1.position.y + 50
  const x2 = level2.position.x + 50
  const y2 = level2.position.y + 50

  // 使用贝塞尔曲线创建平滑路径
  const midX = (x1 + x2) / 2
  const midY = (y1 + y2) / 2
  const controlY = midY - 50

  return `M ${x1} ${y1} Q ${midX} ${controlY} ${x2} ${y2}`
}

function handleLevelClick(level: Level) {
  if (level.status === 'locked') {
    soundManager.playError()
    return
  }

  soundManager.playClick()

  // 跳转到对应的关卡页面
  router.push(`/level/${level.id}`)
}
</script>

<style scoped>
.map-view {
  min-height: 100vh;
  width: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
  padding: 30px 20px;
  box-sizing: border-box;
  margin: 0;
}

.map-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40px;
  padding: 0 40px;
  width: 100%;
}

.map-title {
  font-size: 56px;
  font-weight: 800;
  color: #fff;
  text-shadow: 2px 2px 8px rgba(0, 0, 0, 0.3);
  margin: 0;
  letter-spacing: 2px;
}

.player-info {
  display: flex;
  gap: 20px;
  align-items: center;
}

.stars-count {
  font-size: 28px;
  font-weight: bold;
  color: #fff;
  background: rgba(255, 255, 255, 0.25);
  backdrop-filter: blur(10px);
  padding: 12px 28px;
  border-radius: 50px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
}

.backpack-btn {
  font-size: 20px;
  padding: 12px 28px;
  background: linear-gradient(135deg, #FF6B6B 0%, #FF8E53 100%);
  color: #fff;
  border: none;
  border-radius: 50px;
  cursor: pointer;
  font-weight: bold;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(255, 107, 107, 0.4);
}

.backpack-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(255, 107, 107, 0.6);
}

.map-container {
  position: relative;
  width: 100%;
  height: 650px;
  background: rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(10px);
  border-radius: 30px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.map-content {
  position: relative;
  width: 100%;
  height: 100%;
  padding: 20px;
}

.map-path {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.level-path {
  fill: none;
  stroke: #ccc;
  stroke-width: 8;
  stroke-linecap: round;
  stroke-dasharray: 10, 5;
}

.level-path--unlocked {
  stroke: #4CAF50;
  stroke-dasharray: none;
}

.level-node {
  position: absolute;
  width: 120px;
  height: 120px;
  transform: translate(-50%, -50%);
  cursor: pointer;
  transition: all 0.3s ease;
}

.level-node:hover:not(.level-node--locked) {
  transform: translate(-50%, -50%) scale(1.15);
}

.level-icon {
  width: 120px;
  height: 120px;
  background: linear-gradient(135deg, #ffffff 0%, #f5f5f5 100%);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 56px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.25);
  border: 5px solid #4CAF50;
  transition: all 0.3s ease;
}

.level-node:hover:not(.level-node--locked) .level-icon {
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.35);
}

.level-node--locked .level-icon {
  background: linear-gradient(135deg, #e0e0e0 0%, #bdbdbd 100%);
  border-color: #999;
  filter: grayscale(100%);
  opacity: 0.6;
}

.level-node--completed .level-icon {
  border-color: #FFD700;
  background: linear-gradient(135deg, #FFF9C4 0%, #FFE082 100%);
  box-shadow: 0 8px 25px rgba(255, 215, 0, 0.4);
}

.level-name {
  text-align: center;
  margin-top: 12px;
  font-size: 15px;
  font-weight: 700;
  color: #fff;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(5px);
  padding: 8px 14px;
  border-radius: 20px;
  white-space: nowrap;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.level-stars {
  display: flex;
  justify-content: center;
  gap: 5px;
  margin-top: 5px;
}

.star {
  font-size: 20px;
  filter: grayscale(100%);
  opacity: 0.3;
}

.star--filled {
  filter: none;
  opacity: 1;
}

.level-lock {
  position: absolute;
  top: 0;
  right: 0;
  font-size: 32px;
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.modal-content {
  background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
  border-radius: 30px;
  padding: 50px;
  max-width: 700px;
  width: 90%;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  animation: slideUp 0.4s ease;
}

@keyframes slideUp {
  from {
    transform: translateY(50px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.backpack-modal h2 {
  font-size: 42px;
  font-weight: 800;
  margin: 0 0 30px 0;
  text-align: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.backpack-modal h3 {
  font-size: 32px;
  font-weight: 700;
  margin: 40px 0 25px 0;
  color: #333;
}

.backpack-stats {
  background: linear-gradient(135deg, #667eea15 0%, #764ba215 100%);
  padding: 25px;
  border-radius: 20px;
  margin-bottom: 30px;
  border: 2px solid rgba(102, 126, 234, 0.2);
}

.stat-item {
  display: flex;
  justify-content: space-between;
  font-size: 22px;
  margin: 15px 0;
  padding: 10px 0;
}

.stat-label {
  font-weight: 700;
  color: #555;
}

.stat-value {
  color: #667eea;
  font-weight: 800;
  font-size: 24px;
}

.badges-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 25px;
  margin-bottom: 40px;
}

.badge-item {
  background: linear-gradient(135deg, #FFF9C4 0%, #FFE082 100%);
  padding: 25px;
  border-radius: 20px;
  text-align: center;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(255, 215, 0, 0.3);
  cursor: pointer;
}

.badge-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(255, 215, 0, 0.5);
}

.badge-item--locked {
  background: linear-gradient(135deg, #e0e0e0 0%, #bdbdbd 100%);
  filter: grayscale(100%);
  opacity: 0.4;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.badge-item--locked:hover {
  transform: none;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.badge-icon {
  font-size: 56px;
  margin-bottom: 12px;
}

.badge-name {
  font-size: 19px;
  font-weight: 800;
  margin-bottom: 8px;
  color: #333;
}

.badge-desc {
  font-size: 14px;
  color: #666;
  line-height: 1.4;
}

.close-btn {
  width: 100%;
  padding: 18px;
  font-size: 22px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  border: none;
  border-radius: 50px;
  cursor: pointer;
  font-weight: 700;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.4);
}

.close-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.6);
}
</style>
